package com.ljkj.cordial.chat.ui.views;

import com.lanjiu.lib.imclient.bus.EventConstants;
import com.lanjiu.lib.imclient.bus.MessageEvent;
import com.lanjiu.lib.imclient.dto.base.Dto;
import com.lanjiu.lib.imclient.dto.extra.RegisteredUserSettingsDto;
import com.lanjiu.lib.imclient.dto.user.RegisteredUserDto;
import com.lanjiu.lib.imclient.dto.user.UserDto;
import com.lanjiu.lib.imclient.user.UserPreferences;
import com.lanjiu.lib.utils.log.Logger;
import com.ljkj.cordial.chat.ui.IView;

/**
 * @description:
 * @author: Panwei
 * @date: 2019/11/22 14:24
 */
public class ModifyPhoneView extends IView {

    private ModifyPhoneCallback mCallback;

    public ModifyPhoneView(ModifyPhoneCallback mCallback) {
        this.mCallback = mCallback;
    }

    public void doGetVerificationCode(String phone) {
        doSendRequest(EventConstants.Action.VERIFICATION_CODE, phone);
    }

    public void doSetupPhone(String phone, String captcha) {
        RegisteredUserDto registeredUser = new RegisteredUserDto();
        registeredUser.setUserId(UserPreferences.getUserId());
        registeredUser.setPhone(phone);
        registeredUser.setCaptcha(captcha);
        doSendRequest(EventConstants.Action.SETUP_PHONE, registeredUser.toJson());
    }

    public void doModifyPhone(String phone, String captcha, String newPhone, String newCaptcha) {
        RegisteredUserDto registeredUser = new RegisteredUserDto();
        registeredUser.setUserId(UserPreferences.getUserId());
        registeredUser.setPhone(phone);
        registeredUser.setCaptcha(captcha);
        RegisteredUserDto newRegisteredUser = new RegisteredUserDto();
        newRegisteredUser.setUserId(UserPreferences.getUserId());
        newRegisteredUser.setPhone(newPhone);
        newRegisteredUser.setCaptcha(newCaptcha);
        RegisteredUserSettingsDto userSettings = new RegisteredUserSettingsDto(registeredUser, newRegisteredUser);
        doSendRequest(EventConstants.Action.SETUP_PHONE_EDIT, userSettings.toJson());
    }

    @Override
    public void onMessageEventHandle(MessageEvent event) {
        Logger.print("ModifyPhoneView", event.toString());
        if (event.action() == EventConstants.Action.VERIFICATION_CODE) {
            String phone = null;
            if (checkIsSuccess(event.code(), event.data())) {
                RegisteredUserDto registeredUser = Dto.fromJson(event.data(), RegisteredUserDto.class);
                if (null != registeredUser) phone = registeredUser.getPhone();
            }
            if (null != mCallback)
                mCallback.onVerifyCodeCallback(event.code(), event.msg(), phone);
        } else if (event.action() == EventConstants.Action.SETUP_PHONE) {
            UserDto user = null;
            if (checkIsSuccess(event.code(), event.data())) {
                RegisteredUserDto registeredUser = Dto.fromJson(event.data(), RegisteredUserDto.class);
                if (null != registeredUser) user = new UserDto(registeredUser);
            }
            if (null != mCallback)
                mCallback.onSetupPhoneCallback(event.code(), event.msg(), user);
        } else if (event.action() == EventConstants.Action.SETUP_PHONE_EDIT) {
            UserDto user = null;
            if (checkIsSuccess(event.code(), event.data())) {
                RegisteredUserDto registeredUser = Dto.fromJson(event.data(), RegisteredUserDto.class);
                if (null != registeredUser) user = new UserDto(registeredUser);
            }
            if (null != mCallback)
                mCallback.onModifyPhoneCallback(event.code(), event.msg(), user);
        }
    }

    public interface ModifyPhoneCallback {

        void onVerifyCodeCallback(int code, String msg, String phone);

        void onSetupPhoneCallback(int code, String msg, UserDto data);

        void onModifyPhoneCallback(int code, String msg, UserDto data);
    }
}
